package server;

import control.CControl;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class SServerConnectionPanel extends JPanel implements ActionListener, Observer
{
    private CControl cControl;
    
    private JTextField portTextField;
    private JButton connectionButton;
    
    private boolean isServerWorking = false;
    
    public SServerConnectionPanel(CControl cControl)
    {
        this.cControl = cControl;
        
        initComponent();
        
        setBackground(new Color(0xC95D5D)); // Red
        
        String port = cControl.getSettingsParameter("serverport");
        portTextField.setText(port);
        
        this.cControl.addServerStatusObserver(this);
    }
    
    @Override
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if(source == connectionButton)
        {
            if (isServerWorking && "Stop".equals(connectionButton.getText()))
            {
                cControl.stopServer();
                connectionButton.setText("Start");
            }
            else if (( ! isServerWorking) && "Start".equals(connectionButton.getText()))
            {
                try
                {
                    cControl.startServer(Integer.parseInt(portTextField.getText()));
                    connectionButton.setText("Stop");
                    cControl.setSettingsParameter("serverport", portTextField.getText());
                }
                
                catch (IllegalArgumentException ex)
                {
                    JOptionPane.showMessageDialog(this,
                        "Wrong port number. The number should be an integer between 1 and 65000",
                        "Wrong port number", JOptionPane.WARNING_MESSAGE);
                }
            }
        }
    }

    @Override
    public void update(Observable o, Object arg)
    {
        if (arg instanceof String)
        {
            String status = (String) arg;
            System.out.println("Received server status: " + status);
            switch (status)
            {
                case SNewConnectionsHandler.STATUS_RUNNING:
                    setBackground(new Color(0xD0EB87)); // Green
                    connectionButton.setText("Stop");
                    isServerWorking = true;
                
                    break;
                case SNewConnectionsHandler.STATUS_STOPPED:
                    setBackground(new Color(0xC95D5D)); // Red
                    connectionButton.setText("Start");
                    isServerWorking = false;
                    break;
            }
		}
    }
    
    private void initComponent()
    {
        portTextField = new JTextField();
        portTextField.setColumns(6);
        connectionButton = new JButton("Start");
        connectionButton.addActionListener(this);
        
        GridBagConstraints c = new GridBagConstraints();
        
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 3;
        c.ipady = 10;
        c.fill = GridBagConstraints.HORIZONTAL;
        add(new JLabel("Web Server"), c);
        
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 1;
        c.ipady = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.1;
        JLabel label = new JLabel("Port");
        label.setHorizontalAlignment(JLabel.RIGHT);
        add(label, c);

        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 1;
        c.ipady = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.6;
        add(portTextField, c);
        
        c.gridx = 2;
        c.gridy = 1;
        c.gridwidth = 1;
        c.gridheight = 2;
        c.ipady = 0;
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 0.3;
        add(connectionButton, c);
    }
}
